package com.springboot.study.utils;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @Author: guodong
 * @Date: 2020/8/17 16:37
 * @Version: 1.0
 * @Description:
 */
@Slf4j
public class LocalDateUtils {

    /**
     * Date转LocalDate
     * @param date
     */
    public static LocalDate date2LocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }


    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }


    /**
     * 获取两个日期之间的天数算头不算尾
     * @param startDay 开始日期
     * @param endDay 结束日期
     * @return
     */
    public static List<LocalDate> getBetweenDays(LocalDate startDay, LocalDate endDay){
        List<LocalDate> localDateList = new ArrayList<>();
        long subtractDays = Math.abs(endDay.until(startDay, ChronoUnit.DAYS));
        if(0 == subtractDays){
            return Arrays.asList(startDay);
        }
        for(int i = 0; i <= subtractDays; i++){
            localDateList.add(startDay.plusDays(i));
        }
        return localDateList;
    }


}
